package com.sgb.kjwl.view.ui.fragment.freightTransport;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLogisticsListBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.NeedsEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.activity.logistics.LogisticsSearchActivity3;
import com.sgb.kjwl.view.ui.adapter.home.SourceOfSupplyAdapter;
import com.sgb.kjwl.view.ui.adapter.home.VehicleOwnerAdapter;
import com.sgb.kjwl.viewmodel.freightTransport.LogisticsListModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * 2020-3-20  产品朱陶丽：新版本
 * Description:
 * Author CFK
 */
public class LogisticsSearchListFragment3 extends AppFragment {
    @Inject
    LogisticsListModel3 model;
    private FragmentLogisticsListBinding binding;
    public static final String KEY_TYPE = "type";
    private int type = 0;
    private LogisticsSearchActivity3 activity;
    private VehicleOwnerAdapter vehicleOwnerAdapter;
    private SourceOfSupplyAdapter sourceOfSupplyAdapter;

    public static LogisticsSearchListFragment3 newInstance(int type) {
        LogisticsSearchListFragment3 fragment = new LogisticsSearchListFragment3();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (LogisticsSearchActivity3) context;
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            View decor = getActivity().getWindow().getDecorView();
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_logistics_list, container, false);
        View decor = getActivity().getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        init();
    }

    private void initView() {
        type = (int) getArguments().get(KEY_TYPE);
        if (type == 1){
            if (vehicleOwnerAdapter == null) {
                vehicleOwnerAdapter = new VehicleOwnerAdapter(getActivity(), null);
            }
            binding.rlvOption.setAdapter(vehicleOwnerAdapter);
        }else {
            if (sourceOfSupplyAdapter == null) {
                sourceOfSupplyAdapter = new SourceOfSupplyAdapter(getActivity(), null);
            }
            binding.rlvOption.setAdapter(sourceOfSupplyAdapter);
        }
        binding.rlvOption.setLayoutManager(new GridLayoutManager(getActivity(), 1));

        binding.rlvOption.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                    model.getNeedsList("","",1);
            }

            @Override
            public void onLoadMore() {
                    model.getMoreList("","",1);
            }
        });
    }


    private void init() {
        model.getNeedsList("","",1);
    }

    public void refresh(List<NeedsEntity> datas, boolean isLast) {

        /*binding.empty.setVisibility(View.GONE);
        binding.rlvOption.setVisibility(View.VISIBLE);
        binding.rlvOption.setLoadingMoreEnabled(true);
        if(type == 1){
            vehicleOwnerAdapter.setDatas(datas);
        }else {
            sourceOfSupplyAdapter.setDatas(datas);
        }
        binding.rlvOption.refreshComplete();
        if (isLast) {
            binding.rlvOption.setLoadingMoreEnabled(false);
        }*/
    }

    public void empty() {
        binding.rlvOption.refreshComplete();
//        binding.rlvView.setVisibility(View.GONE);
        if(type == 1){
            vehicleOwnerAdapter.setDatas(null);
        }else {
            sourceOfSupplyAdapter.setDatas(null);
        }
        binding.empty.setVisibility(View.VISIBLE);
    }

    public void loadMore(List<NeedsEntity> datas, boolean isLast) {
       /* if(type == 1){
            vehicleOwnerAdapter.addDatas(datas);
        }else {
            sourceOfSupplyAdapter.addDatas(datas);
        }
        binding.rlvOption.loadMoreComplete();
        if (isLast) {
            binding.rlvOption.setLoadingMoreEnabled(false);
        }*/
    }

}
